//
//  CheckVerificationPicView.m
//  liBaQiang
//
//  Created by 奇林刘 on 2018/11/8.
//  Copyright © 2018年 liu. All rights reserved.
//

#import "CheckVerificationPicView.h"
#define VerifyPicUrl @"http://luma.beituo.com/mobile/login/verify1"

@interface CheckVerificationPicView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *whiteBoard;
@property (nonatomic, strong) BaseButton *veriPicButton;
@property (nonatomic, strong) UIView *line;

@end

@implementation CheckVerificationPicView

- (void)configSelf {
    self.fp_backgroundColor([[UIColor blackColor] colorWithAlphaComponent:0.5]);
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

- (void)allocateComponents {
    self.fp_addSubview(self.whiteBoard = [[UIView alloc] init]);
    [self.whiteBoard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@(RPX(318)));
        make.height.equalTo(@(RPX(144)));
    }];
    
    self.fp_addSubview(self.titleLabel = [[UILabel alloc] init]);
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.whiteBoard).offset(RPX(20));
    }];
    
    self.fp_addSubview(self.veriPicButton = [[BaseButton alloc] init]);
    [self.veriPicButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(RPX(80)));
        make.height.equalTo(@(RPX(37)));
        make.right.equalTo(self.whiteBoard).offset(RPX(-60));
        make.top.equalTo(self.whiteBoard).offset(RPX(51));
    }];
    
    self.fp_addSubview(self.textField = [[UITextField alloc] init]);
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(RPX(110)));
        make.height.equalTo(@(RPX(37)));
        make.centerY.equalTo(self.veriPicButton);
        make.right.equalTo(self.veriPicButton.mas_left).offset(RPX(-9));
    }];
    
    self.fp_addSubview(self.line = [[UIView alloc] init]);
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.whiteBoard);
        make.height.equalTo(@(1));
        make.bottom.equalTo(self.whiteBoard).offset(RPX(-40));
    }];
    
    self.fp_addSubview(self.okButton = [[UIButton alloc] init]);
    [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_bottom);
        make.left.right.bottom.equalTo(self.whiteBoard);
    }];
}

- (void)configComponents {
    self.titleLabel.fp_text(@"提示").fp_font(RPXFont(14));
    self.whiteBoard.fp_backgroundColor([UIColor whiteColor]).fp_cornerRadius(RPX(3));
    self.veriPicButton.fp_addTargetActionForControlEvents(self, @selector(onVeriPicButtonTap), UIControlEventTouchUpInside);
    [self onVeriPicButtonTap];
    self.textField.font = RPXFont(14);
    self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RPX(6), 0)];
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField
        .fp_borderColor(HexadecimalColor(@"F5BE6A"))
        .fp_borderWidth(RPX(2))
        .fp_cornerRadius(RPX(2));
    self.line.fp_backgroundColor(HexadecimalColor(@"DDDDDD"));
    [self.okButton setTitleColor:HexadecimalColor(@"009EFF") forState:UIControlStateNormal];
    [self.okButton setTitle:@"确定" forState:UIControlStateNormal];
    self.okButton.titleLabel.font = RPXFont(12);
}

- (void)onVeriPicButtonTap {
    NSInteger rand = [HandyMethods getRandomNumber:0 to:999];
    NSInteger timeStamp = [NSDate date].timeIntervalSince1970;
    self.veriID = [NSString stringWithFormat:@"%zd%zd", timeStamp, rand];
    NSURL *veriUrl = [NSURL URLWithString:[VerifyPicUrl stringByAppendingString:[NSString stringWithFormat:@"&verify_id=%@", self.veriID]]];
    [self.veriPicButton.backgroundImageView sd_setImageWithURL:veriUrl placeholderImage:[UIImage imageNamed:@"second"]];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];;
}

@end
